home *** CD-ROM | disk | FTP | other *** search
- Path: news.connect.net!usenet
- From: Roy Ivy III <roy@wizweb.com>
- Newsgroups: comp.lang.c++
- Subject: "finally" in C++
- Date: Thu, 18 Apr 1996 10:34:37 -0500
- Organization: Wizards of the Web
- Message-ID: <3176610D.6FAD@wizweb.com>
- NNTP-Posting-Host: loish.wizweb.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01Gold (Win95; I)
-
- I'm trying to write a finally mechanism in C++. [Yes, I know that destructors do such things just fine, but I have reason
- to get this working...]. I have a method which works but costs an exception throw _every_ time through the code whether or
- not an exceptional condition has arisen.
-
- Here's the method (macros shown - "-" marks end of particular macro):
-
- foo()
- {
- // WITHFINALLY
- try { int _had_ex_ = 1;
- // -
- // TRY
- try {
- // -
- fX(); // may throw any exception [including unknown execeptions]
- // ENDTRY
- }
- // -
- // CATCH (x)
- catch (x &)
- // -
- { ... }
- // ENDCATCH
- // -
- // FINALLY
- _had_ex_ = 0;
- throw "Do Finally";
- }
- catch (...)
- {
- // -
- { ... <finalize code> ... }
- // ENDFINALLY
- if (_had_ex_) throw;
- }
- // -
-
- return;
- }
-
-
- Anyone have a better idea in which you only have to write the finalization code in one place, and doesn't cost an exception
- to execute the finalization code in the normal (non-exceptional) case?
- [You must assume the the finaliation code _cannot_ be done by a destructor!]
-
- Anyone?
-
- Post and email me any ideas.
-
- Thanks.
-
- - Roy Ivy III
-